package com.happystudy.serviceImpl;

import com.happpystudy.model.course.Course;
import com.happpystudy.model.courseItem.CourseTopicItem;
import com.happpystudy.service.CourseTopicItemService;
import com.happpystudy.service.CourseTopicService;
import com.happpystudy.service.FileService;
import com.happystudy.mapper.CourseTopicItemMapper;
import com.happystudy.util.KeyUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;


@Service
public class CourseTopicItemServiceImpl implements CourseTopicItemService {

    @Resource
    FileService fileService;

    @Resource
    CourseTopicItemMapper courseTopicItemMapper;


    @Resource
    RedisTemplate redisTemplate;


    @Override
    @Transactional
    public int doCreateCourseTopicItem(CourseTopicItem courseTopicItem, MultipartFile file) {
        int result=0;
        String fullPath=null;
        if (file!=null){// && !file.isEmpty()
            try {
                fullPath  = fileService.doSave(file);
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
        }
        int nextKey = this.getNextPrimaryKey();
        courseTopicItem.setItemId(nextKey);
        if (fullPath!=null){
            courseTopicItem.setAttachment(file.getOriginalFilename());
            courseTopicItem.setUuid(fullPath);
            courseTopicItem.setContentType(file.getContentType());
        }
        result = this.insertCourseTopicItem(courseTopicItem);
        System.out.println(courseTopicItem);
        return result;
    }

    @Override
    @Transactional
    public int doUpdateCourseTopicItem(CourseTopicItem courseTopicItem, MultipartFile file) {
        int result=0;
        String fullPath=null;
        if (file!=null){// && !file.isEmpty()
            try {
                fullPath  = fileService.doSave(file);
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
        }

        if (fullPath!=null){
            courseTopicItem.setAttachment(file.getOriginalFilename());
            courseTopicItem.setUuid(fullPath);
            courseTopicItem.setContentType(file.getContentType());
        }
        result = this.updateCourseTopicItem(courseTopicItem);
        System.out.println(courseTopicItem);
        return result;
    }


    @Override
    @Transactional
    public int insertCourseTopicItem(CourseTopicItem courseTopicItem) {
        int result;
        result = courseTopicItemMapper.insertSelective(courseTopicItem);
        if (result>0){
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CourseTopicItem.class));
            String key = KeyUtil.COURSETOPICITEM_BY_TOPIC_ID + ":" +courseTopicItem.getTopicId();
            redisTemplate.opsForList().leftPush(key,courseTopicItem);

            key = KeyUtil.COURSETOPICITEM_BY_ITEM_ID + ":" + courseTopicItem.getItemId();
            redisTemplate.opsForValue().set(key,courseTopicItem);
        }
        return result;
    }

    @Override
    @Transactional
    public int updateCourseTopicItem(CourseTopicItem courseTopicItem) {
        int result;
        result = courseTopicItemMapper.updateByPrimaryKeySelective(courseTopicItem);
        if (result>0){
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(CourseTopicItem.class));
            String key = KeyUtil.COURSETOPICITEM_BY_TOPIC_ID + ":" +courseTopicItem.getTopicId();
            redisTemplate.opsForList().leftPush(key,courseTopicItem);

            key = KeyUtil.COURSETOPICITEM_BY_ITEM_ID + ":" + courseTopicItem.getItemId();
            redisTemplate.opsForValue().set(key,courseTopicItem);
        }
        return result;
    }


    @Override
    @Transactional
    public int getNextPrimaryKey() {
        return courseTopicItemMapper.queryNextPrimaryKey();
    }

    @Override
    public List<CourseTopicItem> queryAllCourseTopicItemByTopicId(Integer topicId) {

        List<CourseTopicItem> courseTopicItems = courseTopicItemMapper.selectByTopicId(topicId);

        return courseTopicItems;
    }

    @Override
    public CourseTopicItem queryCourseTopicItemByItemId(Integer itemId){
        return courseTopicItemMapper.selectByPrimaryKey(itemId);
    }

    @Override
    public int deleteCourseItemByKey(Integer itemId) {

        return courseTopicItemMapper.deleteByPrimaryKey(itemId);
    }

    @Override
    public int deleteByTopicId(Integer topicId) {
        return courseTopicItemMapper.deleteByTopicId(topicId);
    }
}
